<?php 
require_once("../includes/config.php"); 
//if recipet added trough ajax
  if(isset($_POST['date']))
  {
    $amount = round($_POST['amount'],2); 
    $result = query("INSERT INTO service_vouchers (contract_id, payer, recipient, amount,paymen_method,reason
	                 ,notes,miladi_date,owner_id) VALUES (?,?,?,?,?,?,?,?,?) ", 
      $_POST['contract_id'],$_POST['payeeee'],$_POST['recipient'],$amount,$_POST['type'],$_POST['reason'],$_POST['notes'],$_POST['date'],$_SESSION['user_id']);
    $id = query("getlastid");
	dump($id);
      //if insurance added trough ajax
   
      $updatePaid = query("UPDATE contracts SET services_revenue = services_revenue + ? where contract_id = ? ",$amount, $_POST['contract_id']);
	   $_SESSION['recent_contract'] =$_POST['contract_id'] ; 
	  $_SESSION['success_message_pop'] = "تم اضافة الخدمه بنجاح";
	  echo 0;
    }
     else
  {
  
    $length = 20;
    
    if( isset($_GET['page'])  && !empty($_GET['page'] ))
    {
      $page =  filter_var($_GET['page'], FILTER_SANITIZE_NUMBER_INT);
      $start = $page*20 - 20;
    }
    else
    {
      $start = 0;
      $page = 1;
    }

    $payments= query("SELECT * FROM service_vouchers WHERE owner_id= ? LIMIT ?,? ",$_SESSION['user_id'] ,$start,$length);
    $sqlCount = query("SELECT COUNT(*) FROM service_vouchers WHERE owner_id = ?",$_SESSION['user_id']);
    
    $totalPages = $sqlCount[0]['COUNT(*)'];
   
    render("service_vouchers.php", array("title"=>"إدارة سندات الخدمات","page"=>$page,"totalPages"=>$totalPages,"payments"=>$payments,"start"=>$start,"length"=>$length));
  }
?>